<%# encoding: utf-8%>
$(function() {
  if ($('body').hasClass('sessions')) {
    if (typeof target_chosen == 'undefined') {
      target_chosen = $('.target-chosen').chosen({'no_results_text': I18n.t('assets.js.no_results')});
    }
    $('#locale-trigger').click(function () {
      $(this).parent().find('#locale-switch').slideToggle('fast');
      $(this).toggleClass('active');
      if ($(this).hasClass('active')) $(this).find('span').html('&#x25B2;');
      else $(this).find('span').html('&#x25BC;');
    });
    $('#new-session-form .help').tooltip({'effect': 'fade', 'tipClass': 'help-tip'});
    $('#new-session-form').validator({'effect': 'below_field_error', 'inputEvent': 'blur', 'lang': validatorTools_lang});
    $('#new-session-form #cloud_service').change(function () {
      var args = $(this).val().match(/{[\d\w\s]+}/i);
      if (args == null) {
        $('#new-session-form tr.args').hide();
      } else {
        $('#new-session-form tr.args').show();
        var new_label = args[0].replace(/{|}/g, '') + ':';
        $('#new-session-form label[for="target_args"]').text(new_label);
      }
    });
    $('#new-session-form #cloud_service').trigger('change');
    $('#new-session-form').submit(function () {
      var cloud_service = $(this).find('#cloud_service').val();
      var args = cloud_service.match(/{[\d\w\s]+}/i);
      if (args == null) {
        var target_url = cloud_service
      } else {
        var target_args = $(this).find('#target_args').val();
        var target_url = cloud_service.replace(/{[\d\w\s]+}/i, target_args);
      }
      $(this).find('#target_url').val(target_url);
      return true;
    });
  }
});